//
//  WebViewController.m
//  MagazineShangejie
//
//  Created by mac on 15/1/15.
//  Copyright (c) 2015年 CnbizMedia. All rights reserved.
//

#import "WebViewController.h"
#import "Util.h"
@interface WebViewController ()<UIWebViewDelegate>{
    UIWebView*webview;
    UIButton*ErrorButton;
}

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD show];
//    self.title=@"我的评论";
    webview=[[UIWebView alloc]init];
    webview.delegate=self;
    [self.view addSubview:webview];
    NSMutableURLRequest *requestM = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:self.url]];
    [requestM addValue:[AppAPIClient sharedClient].cookie forHTTPHeaderField:[AppAPIClient sharedClient].name];
    [requestM addValue:[UIDevice currentDevice].name forHTTPHeaderField:@"X-Device-Name"];
    [requestM addValue:[OpenUDID_UDID value] forHTTPHeaderField:@"X-Device-UDID"];
    NSString *token = [[NSUserDefaults standardUserDefaults] valueForKey:@"token"];
    if (token) {
        [requestM addValue:token forHTTPHeaderField:@"X-Token-Val"];
    }
    NSString *cookie = [AppAPIClient sharedClient].cookie;
    
    cookie = [NSString stringWithFormat:@"%@=%@",[AppAPIClient sharedClient].name,cookie];
    NSLog(@"Cookie:%@",cookie);
    [requestM addValue:cookie forHTTPHeaderField:@"Cookie"];
    NSLog(@"Token:%@,Cookie:%@ - %@",token,[AppAPIClient sharedClient].cookie,[AppAPIClient sharedClient].name);
    requestM = [requestM copy];
    [webview loadRequest:[requestM copy]];
    // Do any additional setup after loading the view.
    
    
    UIButton *buttonf=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonf setFrame:CGRectMake(0, 0, 22, 22)];
    [buttonf addTarget:self action:@selector(popOfDismss) forControlEvents:UIControlEventTouchUpInside];
    [buttonf setImage:[UIImage imageNamed:@"dismiss"] forState:UIControlStateNormal];
    
    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
    if ([newOldYear isEqualToString:@"NewYear"]) {
        //状态栏的文字颜色换成白色
        //        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        
        //            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        [buttonf setImage:[UIImage imageNamed:@"dismissGray"] forState:UIControlStateNormal];
        
    }
  
    UIBarButtonItem*leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:buttonf];
    self.navigationItem.leftBarButtonItem=leftBarItem;
}
-(void)popOfDismss{
    if ([[self.navigationController viewControllers] count]>=2) {
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(void)viewDidLayoutSubviews{
    webview.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
-(void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    //    [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
    [SVProgressHUD dismiss];
    ErrorButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [ErrorButton setImage:[UIImage imageNamed:@"article_logo"] forState:UIControlStateNormal];
    [ErrorButton setFrame:webView.frame];
    [ErrorButton addTarget:self action:@selector(webViewReload) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ErrorButton];
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [webView setUserInteractionEnabled:NO];
    //[SVProgressHUD showWithStatus:LOADING_TEXT maskType:SVProgressHUDMaskTypeGradient];
}
-(void)webViewReload{
    //    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self._Dict[@"url"]]]];
    [webview reload];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
    [webView setUserInteractionEnabled:YES];
    [ErrorButton removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
